import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select

class Base:

    # 封装驱动
    def __init__(self, driver):
        self.driver = driver

    # 封装打开网址
    def goto(self,url):
        self.driver.get(url)
    # 封装定位元素
    def find_element(self, loc):
        return self.driver.find_element(*loc)
    # 封装点击
    def click(self,loc):
        self.find_element(loc).click()
    # 封装输入
    def send_keys(self,loc,text):
        self.find_element(loc).send_keys(text)
    # 封装强制等待
    def sleep(self, timeout):
        time.sleep(int(timeout))
    # 窗口切换
    def new_window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    # 滚动条
    def scrollTo(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')
    # 弹框处理
    def alert(self):
        self.driver.switch_to.alert.accept()
    # iframe处理
    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))
    # 鼠标事件
    def a_click(self,loc):
        ActionChains(self.driver).click(self.find_element(loc)).perform()
    # 键盘事件
    def keys_enter(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)
    # 浏览器事件
    def close_driver(self):
        self.driver.close()
    # 下拉框选择
    def select(self,loc,x):
        select = Select(self.find_element(loc))
        select.select_by_value(x)